package br.cin.stadium.middleware;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import br.cin.stadium.middleware.event.EventRemote;
import br.cin.stadium.middleware.event.EventRemoteListener;
import br.cin.stadium.middleware.media.Media;
import br.cin.stadium.middleware.media.MediaReference;

public class Test extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		

		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		server();
		//client();

		StadiumTS.getInstance().notifyMediaChanges(new ListenerApp(), 1);
	}
	
	private void client()
	{
		try {
			// CLIENTE 
			// read all media from section 1 or with "goal1" name
			Media media = null;
			ArrayList<MediaReference> arraymRef = null;

			//arraymRef = StadiumTS.read(1); // read by stadium section
			// or
			
			arraymRef = StadiumTS.getInstance().read("deadfish"); // read by media name
			// or
			//arraymRef = StadiumTS.readAll(); // read all medias

			if (arraymRef != null)
			{
				for (MediaReference mediaReference : arraymRef)
				{
					Log.i("TEST", "Media " + mediaReference.getMediaName() + " on ip " + mediaReference.getServerLocation().getIp() + "," + mediaReference.getServerLocation().getPort());
					// select a specific media, e.g. the first one
					media = StadiumTS.getInstance().requestMedia(mediaReference);
					// use the media
					Log.i("TEST","Name: " + media.getName() + " |Type: " + media.getType() + " |Sector: " + media.getStadiumSector() + " |Content: " + media.getContent().length);
				}		
			}		
			StadiumTS.getInstance().unRegisterUnit();
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private void server()
	{
		// SERVIDOR
		try {
			Media media = null;
			
			////MEDIA 1
/*			String name = "Goal1_Zico";
			String type = "3gp";
			int stadiumSector = 1;
			byte[] video = new byte[0];

			media = new Media(name, type, stadiumSector, video); // name, type, stadiumSector, content

			// publish the media
			StadiumTS.getInstance().write(media);
*/			

	        ////MEDIA 2		
			InputStream a = this.getResources().openRawResource(R.raw.carlos_alberto_1970);
	        byte[] buffer = null;
	        try
	        {
	            buffer = new byte[a.available()];
	            a.read(buffer);
	        } catch (IOException e)
	        { 
	            //empty
	        }       
	        media = new Media("C_Alberto", "3gp", 2, buffer);
	        StadiumTS.getInstance().write(media);
			
			
	        ////MEDIA 3		
			a = this.getResources().openRawResource(R.raw.zico_1982_voleio);
			buffer = null;
			try
			{
				buffer = new byte[a.available()];
				a.read(buffer);
			} catch (IOException e)
			{ 
			}		
			media = new Media("zico_voleio", "3gp", 2, buffer);
			StadiumTS.getInstance().write(media);
			
		} catch (Exception e) {
			Toast.makeText(this, "ERROR - " + e.getMessage(), Toast.LENGTH_LONG).show();
			System.out.println("ERROR - " + e.getMessage());
			e.printStackTrace();
		}
		
	}
	

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	System.out.println("HANDLER MSG");
			Toast.makeText(Test.this, "NEW MEDIA - ", Toast.LENGTH_LONG).show();
        }
   };
	
	
	
	private class ListenerApp implements EventRemoteListener {

		public void notify(EventRemote eventRemote) {
			handler.sendEmptyMessage(0);
			
			System.out.println("======================================================");
			System.out.println("======================================================");
			System.out.println(eventRemote.getMediaReference());
			System.out.println(eventRemote.getMediaReference().getMediaName());
			System.out.println(eventRemote.getMediaReference().getSector());
		}
		
	}
	
	
}
